<?php

// 信息分类管理

namespace app\console\controller;

use think\Url;
use think\Cache;
use think\Loader;
use think\Request;
use think\Controller;
use app\common\trilateral\Classify as ClassifyAlias;
use app\console\model\InformationType as InformationTypeAlias;

class InformationType extends Controller
{

    protected $beforeActionList = [
        'clearCache' => ['only'],
    ];


    /**
     * 信息分类列表
     */
    public function index()
    {


        return $this->fetch('', [
            'mate_title' => '信息栏目列表',
            'data'       => ClassifyAlias::dimension(Cache::get('informationType'))
        ]);
    }


    /**
     * 信息栏目添加
     */
    public function create(Request $request, InformationTypeAlias $informationType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('InformationType');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $informationType->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('InformationType/index'));
            }

            fail('操作失败');

        }


        return $this->fetch('', [
            "mate_title" => '信息栏目添加',
            'data'       => ClassifyAlias::dimension(Cache::get('informationType'))

        ]);

    }


    /**
     * 栏目编辑
     */
    public function update($id, Request $request, InformationTypeAlias $informationType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('InformationType');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $informationType->allowField(true)->save($data, ['id' => $id]);


            if ($dispose) {
                win('操作成功', Url::build('InformationType/index'));
            }

            fail('操作失败');
        }


        $item = InformationTypeAlias::get($id);


        return $this->fetch('create', [
            "mate_title" => '栏目编辑',
            'data'       => ClassifyAlias::dimension(Cache::get('informationType')),
            'item'       => $item

        ]);
    }


    /**
     * 栏目删除
     */
    public function destroy($id, InformationTypeAlias $informationType)
    {

        if ($informationType->amputate($id)) {

            win('操作成功', Url::build('informationType/index'));
        }


        fail('操作失败');

    }


    /**
     * 栏目全部删除
    */
    public function destroyAll($id)
    {

        if (InformationTypeAlias::amputateAll($id)) {

            win('操作成功', Url::build('informationType/index'));
        }

        fail('操作失败');

    }


    /**
     * 栏目状态改变
     */
    public function auditing(InformationTypeAlias $informationType)
    {
        $id = Request::instance()->param('id');

        $status = $informationType->where('id', $id)->value('status') == '1' ? 2 : 1;

        if ($informationType->where('id', $id)->update(['status' => $status])) {

            return $status;

        }
        return false;
    }


    /**
     * 排列顺序
     */
    public function sorting(InformationTypeAlias $informationType)
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {

            $informationType->where('id', $value)->setField(['sort' => $sort[$key]]);

        }

        win('操作成功', Url::build('informationType/index'));
    }


    /**
     * 清除缓存
     */
    protected function clearCache()
    {
        Cache::rm('informationType');
    }
}